Skip to content

Adding support for logging via swift-log - #324

Open
pfandrade wants to merge 6 commits into
pointfreeco:mainfrom
pfandrade:feature/swift-log
Open

pfandrade wants to merge 6 commits into
pointfreeco:mainfrom
pfandrade:feature/swift-log

Conversation

@pfandrade

Copy link
Copy Markdown

As per discussion #293, this PR adds support for using swift-log for SyncEngine logs.

More precisely it adds a new SQLiteDataSwiftLog trait that allows for conditionally using swift-log instead of OSLog (which remains the default). Since the Logger API of both solutions is essentially the same, very little code needed to change.

Main changes are on Logging.swift, where the check for SQLiteDataSwiftLog is performed, and a typealias SyncEngine.Logger is defined.

One decision I made that I'm not quite sure about is using @_exported in Logging.swift to export the right Logger type. Doing this avoids having to do the #if SQLiteDataSwiftLog dance to import either swift-log or OSLog in any file that uses it1.

Footnotes

  1. I believe, currently, just SyncEngine.swift would need to this import dance. But if logging is needed anywhere else, or if that file gets split up, we'd have to replicate the dance. ↩

pfandrade and others added 2 commits April 17, 2026 15:15
# Conflicts:
#	Package.resolved
#	Package.swift
#	Sources/SQLiteData/CloudKit/SyncEngine.swift
#	Tests/SQLiteDataTests/Internal/BaseCloudKitTests.swift

@stephencelis stephencelis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this! A trait seems like a good way to manage things. Just a few to-dos:

Comment on lines +4 to +5
@_exported import struct Logging.Logger
import protocol Logging.LogHandler

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to export or qualify these imports? Can we simply do:

Suggested change
@_exported import struct Logging.Logger
import protocol Logging.LogHandler
public import Logging

#endif

#if SQLiteDataSwiftLog
struct DisabledLogHandler: Logging.LogHandler {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be private/fileprivate, right?

Suggested change
struct DisabledLogHandler: Logging.LogHandler {
private struct DisabledLogHandler: Logging.LogHandler {

delegate: (any SyncEngineDelegate)? = nil,
logger: Logger = isTesting
? Logger(.disabled) : Logger(subsystem: "SQLiteData", category: "CloudKit")
logger: Logger = isTesting ? Logger.disabled : Logger.default

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be abbreviated?

Suggested change
logger: Logger = isTesting ? Logger.disabled : Logger.default
logger: Logger = isTesting ? .disabled : .default

Comment thread Package.swift
description: "Introduce SQLiteData conformances to the swift-tagged package."
),
.trait(
name: "SQLiteDataSwiftLog",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SPM has converged on a simpler naming convention here:

Suggested change
name: "SQLiteDataSwiftLog",
name: "SwiftLog",

Comment thread Package.swift
.product(
name: "Logging",
package: "swift-log",
condition: .when(traits: ["SQLiteDataSwiftLog"])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
condition: .when(traits: ["SQLiteDataSwiftLog"])
condition: .when(traits: ["SwiftLog"])

#if DEBUG && canImport(CloudKit)
#if canImport(CloudKit)

#if SQLiteDataSwiftLog

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if SQLiteDataSwiftLog
#if SwiftLog

}
#endif

#if SQLiteDataSwiftLog

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if SQLiteDataSwiftLog
#if SwiftLog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants